//
//  YellowPagesManager.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/9/1.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "YellowPagesManager.h"

@implementation YellowPagesManager

/**
 *  请求黄页Ids和列表
 *
 *  @param finished 请求完成后的操作
 */
- (void)requestYellowPagesIdsAndList:(SEGRequestCallBack)finished
{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/convenient/refresh-ids.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,codeType,errorMessage);
            return ;
        }
        
        NSArray* data = [result valueForKey:@"data"];
        if (data.count <= 0) {
            //其他需要显示缺省
            finished(NO, @"", @"数据返回为空！"); //数据返回正常，但是数据为空
        }else {
            //  拼参数ids
            NSMutableString *ids = [NSMutableString stringWithString:@""];
            for (int i = 0; i < data.count; i++) {
                NSString *idString = [[data[i] objectForKey:@"id"] stringValue];
                if (i == 0) {
                    [ids appendString:idString];
                }else {
                    [ids appendFormat:@",%@",idString];
                }
            }
            // 请求列表
            [self requestYellowPageslistWithIds:ids Finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                finished(isSuccessed,codeType,errorMessage);
            }];
        }
    }];
}

/**
 *  请求黄页列表
 *
 *  @param ids      要请求黄页的ID列表
 *  @param finished 请求完成后的操作
 */
- (void)requestYellowPageslistWithIds:(NSString *)ids Finished:(SEGRequestCallBack)finished
{
    NSDictionary* params = @{@"ids" : ids};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/convenient/newlist.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,codeType,errorMessage);
            return ;
        }
        
        NSDictionary *dic = [result valueForKey:@"data"];
        if (!dic || ![dic isKindOfClass:[NSDictionary class]]) {
            finished(NO,codeType,errorMessage);
            return ;
        }
        NSArray* data = [dic valueForKey:@"data"];
        if (!data || ![data isKindOfClass:[NSArray class]]) {
            finished(NO,codeType,errorMessage);
            return ;
        }
        
        NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:data.count];
        NSMutableArray* normalArr = [NSMutableArray array];
        
        for (NSDictionary* dic in data) {
            YellowPagesModel* _model = [YellowPagesModel initWithDictionary:dic];
            [arrM addObject:_model];
            
            if ([_model.isNormal isEqualToNumber:[NSNumber numberWithInteger:1]]) {
                [normalArr addObject:_model];
            }
        }
        
        self.dataSourceNormal = normalArr;
        [self yellowPagesTypeListWithArray:arrM];
        
        finished(YES, codeType, @"");
    }];
}

// 按类型存储列表
- (void)yellowPagesTypeListWithArray:(NSArray *)array
{
    NSMutableArray *nameArr = [NSMutableArray array];
    NSMutableArray *modelArr = [NSMutableArray array];
    
    //获取所有分类
    for (YellowPagesModel *entity in array) {
        NSString *str = entity.categoryName;
        if (![nameArr containsObject:str]) {
            [nameArr addObject:str];
        }
    }

    for (NSString *name in nameArr) {
        NSMutableArray *tempArr = [NSMutableArray array];
        for (YellowPagesModel *entity in array) {
            if ([entity.categoryName isEqualToString:name]) {
                [tempArr addObject:entity];
            }
        }
        [modelArr addObject:tempArr];
    }
    self.dataSourceAllCatalog = nameArr;
    self.dataSourceAllArray = modelArr;
}

@end
